package org.jeecg.modules.inOrder.service.impl;

import org.jeecg.modules.inOrder.entity.InOrder;
import org.jeecg.modules.inOrder.entity.InOrderZi;
import org.jeecg.modules.inOrder.mapper.InOrderZiMapper;
import org.jeecg.modules.inOrder.mapper.InOrderMapper;
import org.jeecg.modules.inOrder.service.IInOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库单主表
 * @Author: jeecg-boot
 * @Date:   2024-06-26
 * @Version: V1.0
 */
@Service
public class InOrderServiceImpl extends ServiceImpl<InOrderMapper, InOrder> implements IInOrderService {

	@Autowired
	private InOrderMapper inOrderMapper;
	@Autowired
	private InOrderZiMapper inOrderZiMapper;
	
	@Override
	@Transactional
	public void saveMain(InOrder inOrder, List<InOrderZi> inOrderZiList) {
		inOrderMapper.insert(inOrder);
		if(inOrderZiList!=null && inOrderZiList.size()>0) {
			for(InOrderZi entity:inOrderZiList) {
				//外键设置
				entity.setInOrderId(inOrder.getId());
				inOrderZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(InOrder inOrder,List<InOrderZi> inOrderZiList) {
		inOrderMapper.updateById(inOrder);
		
		//1.先删除子表数据
		inOrderZiMapper.deleteByMainId(inOrder.getId());
		
		//2.子表数据重新插入
		if(inOrderZiList!=null && inOrderZiList.size()>0) {
			for(InOrderZi entity:inOrderZiList) {
				//外键设置
				entity.setInOrderId(inOrder.getId());
				inOrderZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		inOrderZiMapper.deleteByMainId(id);
		inOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			inOrderZiMapper.deleteByMainId(id.toString());
			inOrderMapper.deleteById(id);
		}
	}
	
}
